package com.ethink.platform.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.order.api.*;
import com.ethink.order.api.enums.*;
import com.ethink.order.api.info.PurchaseReturnOrderModeInfo;
import com.ethink.order.api.info.SaleDemandOrderInfo;
import com.ethink.order.api.info.SaleDemandOrderModeInfo;
import com.ethink.order.api.model.*;
import com.ethink.order.api.request.*;
import com.ethink.platform.common.properties.PlatformCommonProperties;
import com.ethink.platform.controller.order.info.ExportInfo;
import com.ethink.platform.controller.warehouse.MateInventoryOtherOutLogController;
import com.ethink.report.api.info.TocSaleImportJobLogInfo;
import com.ethink.report.common.mapper.MateInventoryOrderQueryMapper;
import com.ethink.report.common.mapper.SaleOrderQueryMapper;
import com.ethink.report.common.mapper.TocSaleImportJobLogMapper;
import com.ethink.warehouse.api.*;
import com.ethink.warehouse.api.enums.*;
import com.ethink.warehouse.api.info.*;
import com.ethink.warehouse.api.request.*;
import com.ethink.warehouse.api.response.*;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component
@Data
public class ExportManager {
    @Autowired
    private PlatformCommonProperties commonProperties;
    @Autowired
    private MdmComponent mdmComponent;
    @Autowired
    private OssTemplateManager ossTemplateManager;
    private ThreadLocal<Map<String, String>> saleChannelCache = new ThreadLocal<>();
    @Autowired
    private SubWarehouseFacade subWarehouseFacade;
    @Autowired
    private SaleDemandOrderFacade saleDemandOrderFacade;
    @Autowired
    private MateInventoryFacade mateInventoryFacade;
    @Autowired
    private MateInventoryDetailOrderFacade mateInventoryDetailOrderFacade;
    @Autowired
    private LogisticsTransferOrderFacade logisticsTransferOrderFacade;
    @Autowired
    private LogisticsTraceOrderFacade logisticsTraceOrderFacade;
    @Autowired
    private TobSaleOrderFacade tobSaleOrderFacade;
    @Autowired
    private SaleOrderFacade saleOrderFacade;
    @Autowired
    private ReportMateInventoryAgeFacade reportMateInventoryAgeFacade;
    @Autowired
    private PurchaseOrderFacade purchaseOrderFacade;
    @Autowired
    private MateInventoryOrderQueryMapper mateInventoryOrderQueryMapper;
    @Autowired
    private TocSaleImportJobLogMapper tocSaleImportJobLogMapper;

    @Autowired
    private SaleOrderQueryMapper saleOrderQueryMapper;

    @Autowired
    private MateInventorySaleChannelChangeLogFacade mateInventorySaleChannelChangeLogFacade;

    @Autowired
    private TobSaleReturnOrderFacade tobSaleReturnOrderFacade;

    @Autowired
    private MateInventoryOtherOutLogFacade mateInventoryOtherOutLogFacade;

    @Autowired
    private MateInventoryOtherInLogFacade mateInventoryOtherInLogFacade;

    @Autowired
    private MateInventoryCxLogFacade mateInventoryCxLogFacade;

    @Autowired
    private PurchaseReturnOrderFacade purchaseReturnOrderFacade;

    @Autowired
    private SaleReturnOrderFacade saleReturnOrderFacade;

    @Autowired
    private MateInventoryTypeChangeLogFacade mateInventoryTypeChangeLogFacade;

    @Autowired
    private MateInventoryAdjustLogFacade mateInventoryAdjustLogFacade;

    private static List<String> querySaleDemandOrderHeaders = Lists.newArrayList(
            "PO单号", "需求单号", "订单来源", "备注", "预计销售总价（含税）", "币种", "汇率", "订单状态", "已锁数量(品)", "未锁数量(品)",
            "已锁行数", "已提报未锁", "未提报", "总行数", "制单人", "制单时间");

    private static List<String> queryMateInventoryHeaders = Lists.newArrayList(
            "商品编码", "商品名称", "存货仓库", "渠道", "库存类型", "在库数量", "可用库存", "锁定库存", "在途数量", "规格",
            "单位", "采购批次", "生产批次", "生产日期", "到期日期", "在仓库龄", "成本单价", "原产国", "最早入库时间", "入库物流跟进单"
    );

    private static List<String> queryMateInventoryBySkuHeaders = Lists.newArrayList(
            "商品编码", "商品名称", "存货仓库", "在库数量", "可用库存", "在途数量", "锁定数量", "规格", "单位"
    );

    private static List<String> queryMateInventoryDetailOrderInHeaders = Lists.newArrayList(
            "入库单号", "入库仓库", "商品编码", "商品名称", "库存状态", "渠道", "采购批次", "入库数量", "来源", "来源单号",
            "申请人", "入库日期", "商品规格", "单位", "生产批次", "原产国", "生产日期", "到期日期", "成本单价", "供应商"
    );

    private static List<String> queryMateInventoryDetailOrderOutHeaders = Lists.newArrayList(
            "出库单号", "出库仓库", "商品编码", "商品名称", "库存状态", "渠道", "采购批次", "出库数量", "来源", "来源单号",
            "申请人", "出库日期", "商品规格", "单位", "生产批次", "原产国", "生产日期", "到期日期", "成本单价", "供应商"
    );

    private static List<String> queryLogisticsTransferOrderHeaders = Lists.newArrayList(
            "调拨单号", "调出仓", "调入仓", "调拨总量", "调拨总额", "调拨类型", "订单状态", "制单人", "制单日期", "备注"
    );

    private static List<String> queryLogisticsTransferOrderGoodsModeHeaders = Lists.newArrayList(
            "调拨单号", "调出仓", "调入仓", "调拨类型", "商品编码", "商品名称", "库存类型", "渠道", "采购批次", "数量",
            "调拨总额", "生产批次", "原产国", "生产日期", "到期日期", "成本价", "规格", "品牌", "单位", "订单状态", "制单人", "制单日期", "备注"
    );

    private static List<String> queryLogisticsTraceOrderGoodsModeHeaders = Lists.newArrayList(
            "物流跟进单号", "来源", "制单时间", "始发国", "始发港", "始发仓", "目的国", "目的港", "目的仓", "制单人", "跟进状态", "备注",
            "原单号", "分仓", "账册", "商品编码", "商品名称", "库存状态", "渠道", "申请数量", "申请人", "采购批次", "生产批次", "生产日期", "到期日期", "成本价"
    );

    private static List<String> listLogisticsTraceOrderRkConfirmDetailHeaders = Lists.newArrayList(
            "*申请详单ID", "原单号", "商品编码", "商品名称", "入库仓库", "账册", "渠道", "申请数量", "跟进数量", "库存状态", "*最终入库",
            "成本单价", "*生产批次", "*生产日期", "*到期日期", "*原产国", "备注"
    );

    private static List<String> queryTobSaleOrderHeaders = Lists.newArrayList(
            "预售单号", "PO单号", "PR单号", "渠道", "发货仓库", "目的地", "预售总量", "结算总量", "退货总量", "未结数量",
            "预售总额", "币种", "订单状态", "制单人", "制单日期", "出库日期", "备注"
    );

    private static List<String> queryTobSaleOrderGoodsModeHeaders = Lists.newArrayList(
            "预售单号", "PO单号", "PR单号", "渠道", "发货仓库", "订单状态", "制单人", "制单日期", "商品编码", "商品名称",
            "采购批次", "库存类型", "数量", "结算数量", "退货入库数量","未结数量", "销售价", "成本价", "币种", "毛利率", "规格", "品牌", "单位", "生产日期", "到期日期",
            "生产批次", "备注", "最早入库时间" ,"在仓库龄", "出库日期"
    );

    private static List<String> querySaleOrderHeaders = Lists.newArrayList(
            "销售单号", "订单来源", "店铺名称", "网店订单号", "备注", "商品总数量", "销售总额", "成本总价", "加权平均天数", "币种", "发货仓库",
            "平台下单时间", "发货时间", "订单类型", "订单状态", "制单人", "制单日期"
    );

    private static List<String> querySaleOrderGoodsModeHeaders = Lists.newArrayList(
            "销售单号", "订单来源", "商品编码", "商品名称", "规格", "品牌", "单位", "采购批次", "生产批次", "库存类型", "出库数量",
            "销售单价", "销售总额", "成本价", "店铺名称", "网店订单号", "备注", "币种", "发货仓库", "订单类型", "订单状态", "制单人", "制单日期", "最早入库日期",
            "出库日期", "周转天数"
    );

    private static List<String> queryPurchaseOrderHeaders = Lists.newArrayList(
            "采购单号", "需求单号", "需求来源", "外部单号", "供应商名称", "备注", "目的仓库", "采购总量", "实入仓总量", "登记少货数量",
            "差异数量", "应付金额(采购总额)", "币种", "汇率", "订单状态", "采购员", "制单日期", "付款时间"
    );

    private static List<String> queryPurchaseOrderModeHeaders = Lists.newArrayList(
            "采购单号", "需求单号", "需求来源", "外部单号", "供应商名称", "备注", "商品编码", "商品名称", "品牌", "规格", "单位", "预计销售价(不含税)",
            "销售币种", "预计毛利", "采购单价", "采购数量", "总额", "目的仓库", "采购币种", "汇率", "订单状态", "实入仓总量", "登记少货数量", "差异数量", "采购员", "制单日期"
    );

    private static List<String> queryReportMateInventoryAgeHeaders = Lists.newArrayList(
            "商品编码", "商品名称", "存货仓库", "渠道", "库存类型", "库存数量", "库存金额", "平均天数", "0-30天数量", "0-30天金额",
            "30-60天数量", "30-60天金额", "60-90天数量", "60-90天金额", "90-180天数量", "90-180天金额", "180-270天数量", "180-270天金额",
            "270-360天数量", "270-360天金额", "360天以上数量", "360天以上金额", "数据创建时间"
    );

    private static List<String> querySalesBatchReasonFailureLogHeaders = Lists.newArrayList(
            "流水号", "渠道", "网店订单号", "失败原因", "仓库", "商品编码", "商品名称", "出库数量", "库存数量"
    );

    private static List<String> queryMateInventorySaleChannelChangeLogHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "变更前渠道", "变更后渠道", "操作数量", "库存成本总价", "交易成本总价", "交易差额总价", "处理状态",
            "制单人", "审批人", "制单日期", "审核日期"
    );

    private static List<String> queryMateInventorySaleChannelChangeLogModeHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "变更前渠道", "变更后渠道", "制单人", "审批人", "制单日期", "处理状态", "备注", "商品编码", "商品名称", "变更数量",
            "库存类型", "库存成本价", "交易成本价", "交易差额", "预计销售时间", "采购批次", "生产批次", "原产国", "生产日期", "到期日期", "审核日期"
    );

    private static List<String> queryTobSaleReturnOrderHeaders = Lists.newArrayList(
            "预售退单号", "预售单号", "PO单号", "备注", "退单状态", "退货仓", "退货总量", "提货地点", "渠道", "制单人", "制单日期"
    );

    private static List<String> queryTobSaleReturnOrderModeHeaders = Lists.newArrayList(
            "预售退单号", "预售单号", "PO单号", "备注", "退单状态", "退货仓", "提货地点", "渠道", "制单人", "制单日期", "商品编码", "商品名称", "退货数量"
    );

    private static List<String> queryMateInventoryOtherOutLogHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "渠道", "操作数量", "库存成本总价", "处理状态", "订单类型", "制单人", "审批人", "制单日期"
    );

    private static List<String> queryMateInventoryOtherInLogHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "渠道", "操作数量", "库存成本总价", "处理状态", "订单类型", "制单人", "审批人", "制单日期"
    );

    private static List<String> queryMateInventoryOtherOutLogModeHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "渠道", "处理状态", "订单类型", "制单人", "审批人", "制单日期", "商品编码", "商品名称", "库存成本价", "出库数量", "出库总额",
            "采购批次", "生产批次", "原产国", "生产日期", "到期日期"
    );

    private static List<String> queryMateInventoryOtherInLogModeHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "渠道", "处理状态", "订单类型", "制单人", "审批人", "制单日期", "商品编码", "商品名称", "库存类型", "库存类型", "生产批次",
            "入库时间", "成本价(美元)", "入库数量", "入库总额", "原产国", "生产日期", "到期日期"
    );

    private static List<String> querySaleDemandOrderModeHeaders = Lists.newArrayList(
            "PO单号", "需求单号", "订单来源", "备注", "币种", "汇率", "订单状态", "制单人", "制单时间", "商品名称", "商品编码", "规格", "商品分类", "品牌", "单位", "需求数量",
            "确认数量", "锁定数量", "最差生产日期", "销售需求交期", "截单时间", "预计销售总价(含税)", "预计销售总价(不含税)", "预计销售价-本币(含税)", "预计销售价-本币(不含税)",
            "税率(%)", "折扣", "目标成本-本币", "预计毛利", "报价数量", "报价状态", "状态"
    );

    private static List<String> queryMateInventoryCxLogHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "拆成几份", "出库数量", "入库数量", "处理状态", "制单人", "制单日期"
    );

    private static List<String> queryMateInventoryCxLogModeHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "处理状态", "制单人", "制单日期", "被拆商品编码", "被拆商品名称", "渠道", "被拆商品成本价", "被拆数量", "拆成几份", "拆后商品编码",
            "拆后商品名称", "被拆商品成本价", "拆后数量", "采购批次", "库存类型", "生产批次", "生产日期", "到期日期"
    );

    private static List<String> queryPurchaseReturnHeaders = Lists.newArrayList(
            "采购退单号", "采购单号", "退款原因", "备注", "退单类型", "退单状态", "供应商名称", "退货金额", "退货总量", "制单人", "制单日期"
    );

    private static List<String> queryPurchaseReturnModeHeaders = Lists.newArrayList(
            "采购退单号", "采购单号", "退款原因", "备注", "退单类型", "退单状态", "供应商名称", "制单人", "制单日期", "商品编码", "商品名称", "品牌",
            "商品分类", "规格", "单位", "采购单价", "采购数量", "少货数量", "总额"
    );

    private static List<String> querySaleReturnOrderHeaders = Lists.newArrayList(
            "销售退单号", "销售单号", "网店订单号", "订单类型", "备注", "退单状态", "退货仓", "退货总量", "制单人", "制单日期"
    );

    private static List<String> querySaleReturnOrderModeHeaders = Lists.newArrayList(
            "销售退单号", "销售单号", "网店订单号", "订单类型", "备注", "退单状态", "退货仓", "制单人", "制单日期", "商品编码", "商品名称", "规格",
            "品牌", "单位", "退货数量"
    );

    private static List<String> queryMateInventoryTypeChangeLogHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "渠道", "变更类型", "操作数量", "库存成本总价", "处理状态", "制单人", "审批人", "制单日期"
    );

    private static List<String> queryMateInventoryTypeChangeLogModeHeaders = Lists.newArrayList(
            "操作流水号", "仓库", "渠道", "变更类型", "处理状态", "制单人", "审批人", "制单日期", "商品编码", "商品名称", "库存成本价", "变更数量",
            "变更总额", "采购批次", "生产批次", "原产国", "生产日期", "到期日期"
    );

    private static List<String> queryMateInventoryAdjustLogHeaders = Lists.newArrayList(
            "矫正单号", "仓库", "原因", "操作数量", "成本总价", "处理状态", "制单人", "审批人", "制单日期"
    );

    private static List<String> queryMateInventoryAdjustLogModeHeaders = Lists.newArrayList(
            "矫正单号", "仓库", "原因", "处理状态", "制单人", "审批人", "制单日期", "商品编码", "商品名称", "渠道", "库存类型", "原采购批次", "原成本价",
            "原生产批次", "原生产日期", "原到期日期", "调整数量", "调整后采购批次", "调整后生产批次", "调整后成本价", "成本价差额", "调整后生产日期", "调整后到期日期"
    );

    public ExportInfo exportSaleDemandOrder(SaleDemandOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        SaleDemandOrderQueryRequest firstPageRequest = new SaleDemandOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<SaleDemandOrderInfo> firstPage = saleDemandOrderFacade.querySaleDemandOrder(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = querySaleDemandOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (SaleDemandOrderInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = infoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            SaleDemandOrderQueryRequest pageRequest = new SaleDemandOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<SaleDemandOrderInfo> page = saleDemandOrderFacade.querySaleDemandOrder(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (SaleDemandOrderInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = infoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "销售需求");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> infoConvertToExcelRow(SaleDemandOrderInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getPoOrderNo());
        objects.add(record.getOrderNo());
        // 订单来源
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getRemark());
        objects.add(record.getTotalPrice());
        objects.add(record.getCurrencyName());
        objects.add(record.getExchangeRate());
        objects.add(SaleDemandOrderOrderStatusEnum.getNameByIntValue(record.getOrderStatus()));
        objects.add(record.getLockQty());
        objects.add(record.getNoLockQty());
        objects.add(record.getLockLine());
        objects.add(record.getPurchaseLine());
        objects.add(record.getNotPurchaseLine());
        objects.add(record.getSumLine());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        return objects;
    }

    public ExportInfo exportMateInventory(MateInventoryQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryQueryRequest firstPageRequest = new MateInventoryQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryDetailInfo> firstPage = mateInventoryFacade.queryMateInventory(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryDetailInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = mateInventoryInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryQueryRequest pageRequest = new MateInventoryQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryDetailInfo> page = mateInventoryFacade.queryMateInventory(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryDetailInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = mateInventoryInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "批次价格库存");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> mateInventoryInfoConvertToExcelRow(MateInventoryDetailInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(record.getSubWarehouseName());
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        objects.add(InventoryTypeEnum.valueOf(record.getInventoryType()).getName());
        objects.add(record.getAvailableQty() + record.getLockQty());
        objects.add(record.getAvailableQty());
        objects.add(record.getLockQty());
        objects.add(record.getOnPassageQty());
        objects.add(record.getProductSpecification());
        objects.add(record.getUnitName());
        objects.add(record.getPurchaseCode());
        objects.add(record.getProduceCode());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        objects.add(record.getInventoryAge());
        objects.add(record.getCostPrice());
        objects.add(record.getCountryOfOrigin());
        objects.add(DateUtil.formatDate(record.getRkTime()));
        objects.add(record.getPurchaseTraceOrderNo());
        return objects;
    }

    public ExportInfo exportMateInventoryBySku(MateInventoryQueryBySkuRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryQueryBySkuRequest firstPageRequest = new MateInventoryQueryBySkuRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryQueryBySkuResponse> firstPage = mateInventoryFacade.queryMateInventoryBySku(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryBySkuHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryQueryBySkuResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = mateInventoryBySkuInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryQueryBySkuRequest pageRequest = new MateInventoryQueryBySkuRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryQueryBySkuResponse> page = mateInventoryFacade.queryMateInventoryBySku(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryQueryBySkuResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = mateInventoryBySkuInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> mateInventoryBySkuInfoConvertToExcelRow(MateInventoryQueryBySkuResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getAvailableQty() + record.getLockQty());
        objects.add(record.getAvailableQty());
        objects.add(record.getOnPassageQty());
        objects.add(record.getLockQty());
        objects.add(record.getProductSpecification());
        objects.add(record.getUnitName());
        return objects;
    }

    public ExportInfo exportMateInventoryDetailOrder(MateInventoryDetailOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryDetailOrderQueryRequest firstPageRequest = new MateInventoryDetailOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryDetailOrderQueryResponse> firstPage = mateInventoryOrderQueryMapper.queryMateInventoryDetailOrder(PageUtils.buildPage(firstPageRequest), firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers;
        if (request.getOrderType().equals(MateInventoryOrderOrderTypeEnum.T1.getIntValue())) {
            headers = queryMateInventoryDetailOrderInHeaders;
        } else {
            headers = queryMateInventoryDetailOrderOutHeaders;
        }
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryDetailOrderQueryResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = mateInventoryDetailOrderInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryDetailOrderQueryRequest pageRequest = new MateInventoryDetailOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryDetailOrderQueryResponse> page = mateInventoryOrderQueryMapper.queryMateInventoryDetailOrder(PageUtils.buildPage(pageRequest), pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryDetailOrderQueryResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = mateInventoryDetailOrderInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url;
        if (request.getOrderType().equals(MateInventoryOrderOrderTypeEnum.T1.getIntValue())) {
            url = ossTemplateManager.upload(baos, "入库单");
        } else {
            url = ossTemplateManager.upload(baos, "出库单");
        }
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> mateInventoryDetailOrderInfoConvertToExcelRow(MateInventoryDetailOrderQueryResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
        if (inventoryTypeEnum != null) {
            objects.add(inventoryTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        objects.add(record.getPurchaseCode());
        objects.add(record.getActualInQty());
        SubjectEnum subjectEnum = SubjectEnum.valueOfCode(record.getSourceOrderType());
        if (subjectEnum != null) {
            objects.add(subjectEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getSourceOrderNo());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getProductSpecification());
        objects.add(record.getUnitName());
        objects.add(record.getProduceCode());
        objects.add(record.getCountryOfOrigin());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        objects.add(record.getCostPrice());
        objects.add(record.getSupplierName());
        return objects;
    }

    public ExportInfo exportLogisticsTransferOrder(LogisticsTransferOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        LogisticsTransferOrderQueryRequest firstPageRequest = new LogisticsTransferOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<LogisticsTransferOrderQueryResponse> firstPage = logisticsTransferOrderFacade.queryLogisticsTransferOrder(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryLogisticsTransferOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (LogisticsTransferOrderQueryResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = logisticsTransferOrderInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            LogisticsTransferOrderQueryRequest pageRequest = new LogisticsTransferOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<LogisticsTransferOrderQueryResponse> page = logisticsTransferOrderFacade.queryLogisticsTransferOrder(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (LogisticsTransferOrderQueryResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = logisticsTransferOrderInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "物流调拨单");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> logisticsTransferOrderInfoConvertToExcelRow(LogisticsTransferOrderQueryResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getOutSubWarehouseName());
        objects.add(record.getInSubWarehouseName());
        objects.add(record.getMateTotalQty());
        objects.add(record.getTotalAmount());
        LogisticsTransferOrderOrderTypeEnum transferOrderOrderTypeEnum = LogisticsTransferOrderOrderTypeEnum.valueOf(record.getOrderType());
        if (transferOrderOrderTypeEnum != null) {
            objects.add(transferOrderOrderTypeEnum.getName());
        } else {
            objects.add(null);
        }
        LogisticsTransferOrderOrderStatusEnum transferOrderOrderStatusEnum = LogisticsTransferOrderOrderStatusEnum.valueOf(record.getOrderStatus());
        if (transferOrderOrderStatusEnum != null) {
            objects.add(transferOrderOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getRemark());
        return objects;
    }

    public ExportInfo exportLogisticsTransferOrderGoodsMode(LogisticsTransferOrderQueryItemModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        LogisticsTransferOrderQueryItemModeRequest firstPageRequest = new LogisticsTransferOrderQueryItemModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<LogisticsTransferOrderQueryItemModeResponse> firstPage = logisticsTransferOrderFacade.queryItemModeLogisticsTransferOrder(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryLogisticsTransferOrderGoodsModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (LogisticsTransferOrderQueryItemModeResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = logisticsTransferOrderGoodsModeInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            LogisticsTransferOrderQueryItemModeRequest pageRequest = new LogisticsTransferOrderQueryItemModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<LogisticsTransferOrderQueryItemModeResponse> page = logisticsTransferOrderFacade.queryItemModeLogisticsTransferOrder(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (LogisticsTransferOrderQueryItemModeResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = logisticsTransferOrderGoodsModeInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "物流调拨单货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> logisticsTransferOrderGoodsModeInfoConvertToExcelRow(LogisticsTransferOrderQueryItemModeResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getOutSubWarehouseName());
        objects.add(record.getInSubWarehouseName());
        LogisticsTransferOrderOrderTypeEnum transferOrderOrderTypeEnum = LogisticsTransferOrderOrderTypeEnum.valueOf(record.getOrderType());
        if (transferOrderOrderTypeEnum != null) {
            objects.add(transferOrderOrderTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
        if (inventoryTypeEnum != null) {
            objects.add(inventoryTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        objects.add(record.getPurchaseCode());
        objects.add(record.getMateTotalQty());
        objects.add(record.getTotalAmount());
        objects.add(record.getProduceCode());
        objects.add(record.getCountryOfOrigin());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        objects.add(record.getCostPrice());
        objects.add(record.getProductSpecification());
        objects.add(record.getBrandName());
        objects.add(record.getUnitName());
        LogisticsTransferOrderOrderStatusEnum transferOrderOrderStatusEnum = LogisticsTransferOrderOrderStatusEnum.valueOf(record.getOrderStatus());
        if (transferOrderOrderStatusEnum != null) {
            objects.add(transferOrderOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getRemark());
        return objects;
    }

    public ExportInfo exportLogisticsTraceOrderGoodsMode(LogisticsTraceOrderQueryGoodsModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        LogisticsTraceOrderQueryGoodsModeRequest firstPageRequest = new LogisticsTraceOrderQueryGoodsModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<LogisticsTraceOrderQueryGoodsModeResponse> firstPage = logisticsTraceOrderFacade.queryLogisticsTraceOrderGoodsMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryLogisticsTraceOrderGoodsModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (LogisticsTraceOrderQueryGoodsModeResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = logisticsTraceOrderGoodsModeInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            LogisticsTraceOrderQueryGoodsModeRequest pageRequest = new LogisticsTraceOrderQueryGoodsModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<LogisticsTraceOrderQueryGoodsModeResponse> page = logisticsTraceOrderFacade.queryLogisticsTraceOrderGoodsMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (LogisticsTraceOrderQueryGoodsModeResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = logisticsTraceOrderGoodsModeInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "物流跟进单货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> logisticsTraceOrderGoodsModeInfoConvertToExcelRow(LogisticsTraceOrderQueryGoodsModeResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getTraceOrderNo());
        SubjectEnum subjectEnum = SubjectEnum.valueOfCode(record.getSourceOrderType());
        if (subjectEnum != null) {
            objects.add(subjectEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(DateUtil.formatDateTime(record.getTraceCreateTime()));
        objects.add(record.getSourceCountry());
        objects.add(record.getSourcePort());
        objects.add(record.getSourceWarehouseName());
        objects.add(record.getTargetCountry());
        objects.add(record.getTargetPort());
        objects.add(record.getTargetWarehouseName());
        objects.add(record.getTraceCreateUserName());
        LogisticsTraceStepEnum logisticsTraceStepEnum = LogisticsTraceStepEnum.valueOfCode(record.getStepStatus());
        if (logisticsTraceStepEnum != null) {
            objects.add(logisticsTraceStepEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getTraceRemark());
        objects.add(record.getSourceOrderNo());
        objects.add(record.getSourceSubWarehouseName());
        objects.add(record.getBkSubWarehouseName());
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
        if (inventoryTypeEnum != null) {
            objects.add(inventoryTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        objects.add(record.getTraceQty());
        objects.add(record.getCreateUserName());
        objects.add(record.getPurchaseCode());
        objects.add(record.getProduceCode());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        objects.add(record.getCostPrice());
        return objects;
    }

    public ExportInfo exportLogisticsTraceOrderRkConfirmDetail(Long logisticsTraceOrderId) {
        LogisticsTraceOrderGetDetailResponse logisticsTraceOrderGetDetail = logisticsTraceOrderFacade.getLogisticsTraceOrderById(logisticsTraceOrderId);
        LogisticsTraceOrderInfo logisticsTraceOrderInfo = logisticsTraceOrderGetDetail.getLogisticsTraceOrderInfo();
        if (!logisticsTraceOrderInfo.getSourceOrderType().equals(SubjectEnum.T11.getCode())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR, "目前只有采购类型的跟进单支持此功能！");
        }
        if (!logisticsTraceOrderInfo.getStepStatus().equals(LogisticsTraceStepEnum.H.getCode())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR, "当前状态不能使用此功能！");
        }

        init();
        ExportInfo exportInfo = new ExportInfo();
        List<LogisticsTraceOrderListRkConfirmDetailResponse> recordList = logisticsTraceOrderFacade.listLogisticsTraceOrderRkConfirmDetail(logisticsTraceOrderId);
        if (CollectionUtils.isEmpty(recordList)) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR, "没有可以导出的数据！");
        }
        List<String> headers = listLogisticsTraceOrderRkConfirmDetailHeaders;
        ExcelWriter excelWriter;
        if (recordList.size() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(recordList)) {
            for (LogisticsTraceOrderListRkConfirmDetailResponse record : recordList) {
                List<Object> row = logisticsTraceOrderRkConfirmDetailInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "物流跟进单入库明细（" + logisticsTraceOrderInfo.getOrderNo() + "）");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> logisticsTraceOrderRkConfirmDetailInfoConvertToExcelRow(LogisticsTraceOrderListRkConfirmDetailResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getApplyDetailId());
        objects.add(record.getSourceOrderNo());
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getBkSubWarehouseName());
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        objects.add(record.getApplyInQty());
        objects.add(record.getTraceQty());
        if (record.getInventoryType() != null) {
            InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
            if (inventoryTypeEnum != null) {
                objects.add(inventoryTypeEnum.getName());
            } else {
                objects.add(null);
            }
        } else {
            objects.add(null);
        }
        objects.add(record.getActualInQty());
        objects.add(record.getCostPrice());
        objects.add(record.getProduceCode());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        objects.add(record.getCountryOfOrigin());
        objects.add(record.getRemark());
        return objects;
    }

    public ExportInfo exportTobSaleOrder(TobSaleOrderPageQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        TobSaleOrderPageQueryRequest firstPageRequest = new TobSaleOrderPageQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<TobSaleOrderModel> firstPage = tobSaleOrderFacade.pageQueryTobSaleOrder(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryTobSaleOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (TobSaleOrderModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = tobSaleOrderInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            TobSaleOrderPageQueryRequest pageRequest = new TobSaleOrderPageQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<TobSaleOrderModel> page = tobSaleOrderFacade.pageQueryTobSaleOrder(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (TobSaleOrderModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = tobSaleOrderInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "预售单");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> tobSaleOrderInfoConvertToExcelRow(TobSaleOrderModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getPoOrderNo());
        objects.add(record.getPrOrderNo());
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getOutSubWarehouseName());
        objects.add(record.getInSubWarehouseName());
        objects.add(record.getPreSaleVolume());
        objects.add(record.getSettlementVolume());
        objects.add(record.getReturnsVolume());
        objects.add(record.getDifferenceQuantity());
        objects.add(record.getPreSaleAmount());
        objects.add(record.getCurrencyName());
        TobSaleOrderOrderStatusEnum tobSaleOrderOrderStatusEnum = TobSaleOrderOrderStatusEnum.valueOf(record.getOrderStatus());
        if (tobSaleOrderOrderStatusEnum != null) {
            objects.add(tobSaleOrderOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getOutboundTime());
        objects.add(record.getRemark());
        return objects;
    }

    public ExportInfo exportTobSaleOrderGoodsMode(TobSaleOrderPageGoodsModeQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        TobSaleOrderPageGoodsModeQueryRequest firstPageRequest = new TobSaleOrderPageGoodsModeQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<TobSaleOrderGoodsModeModel> firstPage = tobSaleOrderFacade.pageQueryTobSaleOrderPageMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryTobSaleOrderGoodsModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (TobSaleOrderGoodsModeModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = tobSaleOrderGoodsModeInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            TobSaleOrderPageGoodsModeQueryRequest pageRequest = new TobSaleOrderPageGoodsModeQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<TobSaleOrderGoodsModeModel> page = tobSaleOrderFacade.pageQueryTobSaleOrderPageMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (TobSaleOrderGoodsModeModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = tobSaleOrderGoodsModeInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "预售单货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> tobSaleOrderGoodsModeInfoConvertToExcelRow(TobSaleOrderGoodsModeModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getPoOrderNo());
        objects.add(record.getPrOrderNo());
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getOutSubWarehouseName());
        TobSaleOrderOrderStatusEnum tobSaleOrderOrderStatusEnum = TobSaleOrderOrderStatusEnum.valueOf(record.getOrderStatus());
        if (tobSaleOrderOrderStatusEnum != null) {
            objects.add(tobSaleOrderOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(record.getPurchaseCode());
        InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
        if (inventoryTypeEnum != null) {
            objects.add(inventoryTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getQty());
        objects.add(record.getSettleQty());
        objects.add(record.getReturnQty());
        objects.add(record.getDifferenceQuantity());
        objects.add(record.getSalePrice());
        objects.add(record.getCostPrice());
        objects.add(record.getCurrencyName());
        objects.add(record.getGrossProfitRate());
        objects.add(record.getProductSpecification());
        objects.add(record.getBrandName());
        objects.add(record.getUnitName());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        objects.add(record.getProduceCode());
        objects.add(record.getRemark());
        objects.add(DateUtil.formatDateTime(record.getRkTime()));
        objects.add(record.getInventoryAge());
        objects.add(record.getOutboundTime());
        return objects;
    }

    public ExportInfo exportSaleOrder(SaleOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        SaleOrderQueryRequest firstPageRequest = new SaleOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        if (ObjectUtil.isNotEmpty(firstPageRequest.getSaleChannelArr())) {
            firstPageRequest.setSaleChannel(firstPageRequest.getSaleChannelArr()[0]);
            if (firstPageRequest.getSaleChannelArr().length > 1) {
                firstPageRequest.setSaleSubChannel(firstPageRequest.getSaleChannelArr()[1]);
            }
        }
        Page<SaleOrderPageQueryModel> firstPage = saleOrderQueryMapper.pageQuery(PageUtils.buildPage(firstPageRequest), firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = querySaleOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (SaleOrderPageQueryModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = saleOrderInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            SaleOrderQueryRequest pageRequest = new SaleOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            if (ObjectUtil.isNotEmpty(pageRequest.getSaleChannelArr())) {
                pageRequest.setSaleChannel(pageRequest.getSaleChannelArr()[0]);
                if (pageRequest.getSaleChannelArr().length > 1) {
                    pageRequest.setSaleSubChannel(pageRequest.getSaleChannelArr()[1]);
                }
            }
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<SaleOrderPageQueryModel> page = saleOrderQueryMapper.pageQuery(PageUtils.buildPage(pageRequest), pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (SaleOrderPageQueryModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = saleOrderInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "销售单");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> saleOrderInfoConvertToExcelRow(SaleOrderPageQueryModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getShopName());
        objects.add(record.getShopOrderNo());
        objects.add(record.getRemark());
        objects.add(record.getMateQty());
        objects.add(record.getTotalOrderPrice());
        objects.add(record.getTotalCost());
        objects.add(record.getWeightAvgDay());
        objects.add(record.getCurrencyName());
        objects.add(record.getSubWarehouseName());
        objects.add(DateUtil.formatDateTime(record.getOrderTime()));
        objects.add(DateUtil.formatDateTime(record.getDeliveryTime()));
        SaleOrderOrderTypeEnum saleOrderOrderTypeEnum = SaleOrderOrderTypeEnum.valueOf(record.getOrderType());
        if (saleOrderOrderTypeEnum != null) {
            objects.add(saleOrderOrderTypeEnum.getName());
        } else {
            objects.add(null);
        }
        SaleOrderOrderStatusEnum saleOrderOrderStatusEnum = SaleOrderOrderStatusEnum.valueOf(record.getOrderStatus());
        if (saleOrderOrderStatusEnum != null) {
            objects.add(saleOrderOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        return objects;
    }

    public ExportInfo exportSaleOrderGoodsMode(SaleOrderModeQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        if (ObjectUtil.isNotEmpty(request.getSaleChannelArr())) {
            request.setSaleChannel(request.getSaleChannelArr()[0]);
            if (request.getSaleChannelArr().length > 1) {
                request.setSaleSubChannel(request.getSaleChannelArr()[1]);
            }
        }
        SaleOrderModeQueryRequest firstPageRequest = new SaleOrderModeQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<SaleOrderModeQueryModel> firstPage = saleOrderQueryMapper.pageSaleOrderModeQuery(PageUtils.buildPage(firstPageRequest), firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = querySaleOrderGoodsModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (SaleOrderModeQueryModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = saleOrderGoodsModeInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            SaleOrderModeQueryRequest pageRequest = new SaleOrderModeQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<SaleOrderModeQueryModel> page = saleOrderQueryMapper.pageSaleOrderModeQuery(PageUtils.buildPage(pageRequest), pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (SaleOrderModeQueryModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = saleOrderGoodsModeInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "销售单货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> saleOrderGoodsModeInfoConvertToExcelRow(SaleOrderModeQueryModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getInternationCode());
        objects.add(record.getMateSkuName());
        objects.add(record.getProductSpecification());
        objects.add(record.getBrandName());
        objects.add(record.getUnitName());
        objects.add(record.getPurchaseCode());
        objects.add(record.getProduceCode());
        InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
        if (inventoryTypeEnum != null) {
            objects.add(inventoryTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getOutQty());
        objects.add(record.getSalePrice());
        objects.add(record.getSumPrice());
        objects.add(record.getCostPrice());
        objects.add(record.getShopName());
        objects.add(record.getShopOrderNo());
        objects.add(record.getRemark());
        objects.add(record.getCurrencyName());
        objects.add(record.getSubWarehouseName());
        SaleOrderOrderTypeEnum saleOrderOrderTypeEnum = SaleOrderOrderTypeEnum.valueOf(record.getOrderType());
        if (saleOrderOrderTypeEnum != null) {
            objects.add(saleOrderOrderTypeEnum.getName());
        } else {
            objects.add(null);
        }
        SaleOrderOrderStatusEnum saleOrderOrderStatusEnum = SaleOrderOrderStatusEnum.valueOf(record.getOrderStatus());
        if (saleOrderOrderStatusEnum != null) {
            objects.add(saleOrderOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(DateUtil.formatDateTime(record.getOldestTime()));
        objects.add(DateUtil.formatDateTime(record.getOutboundTime()));
        objects.add(record.getTurnoverDay());
        return objects;
    }

    public ExportInfo exportReportMateInventoryAge(ReportMateInventoryAgeQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        ReportMateInventoryAgeQueryRequest firstPageRequest = new ReportMateInventoryAgeQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<ReportMateInventoryAgeQueryResponse> firstPage = reportMateInventoryAgeFacade.queryReportMateInventoryAge(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryReportMateInventoryAgeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (ReportMateInventoryAgeQueryResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventoryAgeInfoConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            ReportMateInventoryAgeQueryRequest pageRequest = new ReportMateInventoryAgeQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<ReportMateInventoryAgeQueryResponse> page = reportMateInventoryAgeFacade.queryReportMateInventoryAge(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (ReportMateInventoryAgeQueryResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventoryAgeInfoConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "仓库库龄报表");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventoryAgeInfoConvertToExcelRow(ReportMateInventoryAgeQueryResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(record.getSubWarehouseName());
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        InventoryTypeEnum inventoryTypeEnum = InventoryTypeEnum.valueOf(record.getInventoryType());
        if (inventoryTypeEnum != null) {
            objects.add(inventoryTypeEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getQty());
        objects.add(record.getAmt());
        objects.add(record.getAverageQty());
        objects.add(record.getF0t30Qty());
        objects.add(record.getF0t30Amt());
        objects.add(record.getF30t60Qty());
        objects.add(record.getF30t60Amt());
        objects.add(record.getF60t90Qty());
        objects.add(record.getF60t90Amt());
        objects.add(record.getF90t180Qty());
        objects.add(record.getF90t180Amt());
        objects.add(record.getF180t270Qty());
        objects.add(record.getF180t270Amt());
        objects.add(record.getF270t360Qty());
        objects.add(record.getF270t360Amt());
        objects.add(record.getF360Qty());
        objects.add(record.getF360Amt());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        return objects;
    }

    private String saleChannel(String saleChannel, String saleSubChannel) {
        Map<String, String> map = saleChannelCache.get();
        if (StringUtils.isBlank(saleChannel)) {
            return map == null ? "" : String.valueOf(map.get(saleSubChannel));
        }
        return saleChannel + (map == null ? "" : ("/" + (StringUtils.equals(map.get(saleSubChannel), null)? " 意欣渠道": map.get(saleSubChannel))));
    }

    private void init() {
        saleChannelCache.remove();
        saleChannelCache.set(mdmComponent.queryDictionary("MZ_SUB_SALE_CHANNEL_CODE"));
    }

    public ExportInfo oldExportPurchase(PurchaseOrderPageQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        PurchaseOrderPageQueryRequest firstPageRequest = new PurchaseOrderPageQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<PurchaseOrderModel> firstPage = purchaseOrderFacade.purchaseOrderPageQuery(firstPageRequest);
        MateInventoryDetailOrderQueryRequest queryRequest = new MateInventoryDetailOrderQueryRequest();
        List<String> orderNoList = CollUtil.getFieldValues(firstPage.getRecords(), "orderNo", String.class);
        queryRequest.setSourceOrderNoList(orderNoList);
        queryRequest.setSourceOrderType(SubjectEnum.T11.getCode());
        List<MateInventoryDetailOrderMoreInfo> infoList = mateInventoryDetailOrderFacade.listMateInventoryDetailOrder(queryRequest);
        Map<String, Integer> map = new HashMap<>();
        for (MateInventoryDetailOrderMoreInfo moreInfo : infoList) {
            map.put(moreInfo.getSourceOrderNo(), moreInfo.getMateTotalQty());
        }
        //差异数量 = 采购数量 - 少货数量 - 入库数量
        firstPage.getRecords().stream().map(
                a -> {
                    a.setInboundQuantity(map.get(a.getOrderNo()));
                    a.setDifferenceQuantity(a.getPurchaseTotal() - a.getInboundQuantity() - a.getDifferenceQuantity());
                    return a;
                }
        );
        System.out.println("firstPage");
        firstPage.getRecords().forEach(System.out::println);
        // 2 改这里 下载的header
        List<String> headers = queryPurchaseOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (PurchaseOrderModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = purchaseOrderModelConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            PurchaseOrderPageQueryRequest pageRequest = new PurchaseOrderPageQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<PurchaseOrderModel> page = purchaseOrderFacade.purchaseOrderPageQuery(pageRequest);
            System.out.println("page");
            page.getRecords().forEach(System.out::println);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (PurchaseOrderModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = purchaseOrderModelConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "采购订单");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    public ExportInfo exportPurchase(PurchaseOrderPageQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        PurchaseOrderPageQueryRequest firstPageRequest = new PurchaseOrderPageQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<PurchaseOrderModel> firstPage = purchaseOrderFacade.purchaseOrderPageQuery(firstPageRequest);
        MateInventoryDetailOrderQueryRequest queryRequest = new MateInventoryDetailOrderQueryRequest();
        List<String> orderNoList = CollUtil.getFieldValues(firstPage.getRecords(), "orderNo", String.class);
        queryRequest.setSourceOrderNoList(orderNoList);
        queryRequest.setSourceOrderType(SubjectEnum.T11.getCode());
        List<MateInventoryDetailOrderMoreInfo> infoList = mateInventoryDetailOrderFacade.listMateInventoryDetailOrder(queryRequest);
        Map<String, Integer> map = new HashMap<>();
        for (MateInventoryDetailOrderMoreInfo moreInfo : infoList) {
            map.put(moreInfo.getSourceOrderNo(), moreInfo.getMateTotalQty());
        }
        //差异数量 = 采购数量 - 少货数量 - 入库数量
        firstPage.getRecords().stream().forEach(
                a -> {
                    Optional.ofNullable(map.get(a.getOrderNo())).ifPresent(
                            b -> {
                                a.setInboundQuantity(b);
                                a.setDifferenceQuantity(a.getPurchaseTotal() - b - a.getMissingQuantity());
                            }
                    );
                }
        );
        // 2 改这里 下载的header
        List<String> headers = queryPurchaseOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (PurchaseOrderModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = purchaseOrderModelConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            PurchaseOrderPageQueryRequest pageRequest = new PurchaseOrderPageQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<PurchaseOrderModel> page = purchaseOrderFacade.purchaseOrderPageQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (PurchaseOrderModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = purchaseOrderModelConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "采购订单");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> purchaseOrderModelConvertToExcelRow(PurchaseOrderModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getSaleDemandOrderNo());
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getOutOrderNo());
        objects.add(record.getSupplierName());
        objects.add(record.getRemark());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getPurchaseTotal());
        objects.add(record.getInboundQuantity());
        objects.add(record.getMissingQuantity());
        objects.add(record.getDifferenceQuantity());
        objects.add(record.getShouldPayAmount());
        objects.add(record.getCurrencyName());
        objects.add(record.getExchangeRate());
        PurchaseOrderStatusEnum purchaseOrderStatusEnum = PurchaseOrderStatusEnum.valueOf(record.getOrderStatus());
        if (purchaseOrderStatusEnum != null) {
            objects.add(purchaseOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDate(record.getCreateTime()));
        objects.add(record.getPayTime());
        return objects;
    }

    public ExportInfo exportPurchaseMode(PurchaseOrderPageQueryModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        PurchaseOrderPageQueryModeRequest firstPageRequest = new PurchaseOrderPageQueryModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<PurchaseOrderModeModel> firstPage = purchaseOrderFacade.purchaseOrderPageModeQuery(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryPurchaseOrderModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (PurchaseOrderModeModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = purchaseOrderModeModelConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            PurchaseOrderPageQueryModeRequest pageRequest = new PurchaseOrderPageQueryModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<PurchaseOrderModeModel> page = purchaseOrderFacade.purchaseOrderPageModeQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (PurchaseOrderModeModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = purchaseOrderModeModelConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "采购单货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> purchaseOrderModeModelConvertToExcelRow(PurchaseOrderModeModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getSaleDemandOrderNo());
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(record.getOutOrderNo());
        objects.add(record.getSupplierName());
        objects.add(record.getRemark());
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(record.getBrandName());
        objects.add(record.getProductSpecification());
        objects.add(record.getUnitName());
        objects.add(record.getExceptSalePriceNoTax());
        objects.add(record.getSaleCurrencyName());
        objects.add(record.getExceptGrossProfit());
        objects.add(record.getAveragePurchasePrice());
        objects.add(record.getLockOrderQty());
        objects.add(record.getTotal());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getCurrencyName());
        objects.add(record.getExchangeRate());
        PurchaseOrderStatusEnum purchaseOrderStatusEnum = PurchaseOrderStatusEnum.valueOf(record.getOrderStatus());
        if (purchaseOrderStatusEnum != null) {
            objects.add(purchaseOrderStatusEnum.getName());
        } else {
            objects.add(null);
        }
        objects.add(record.getActualInQty());
        objects.add(record.getLessQty());
        objects.add(record.getDifferenceQty());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDate(record.getCreateTime()));
        return objects;
    }

    public ExportInfo exportReasonFailureLog(Integer logId) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        // 1 改这里 分页查询方法
        List<TocSaleImportJobLogInfo> tocSaleImportJobLogInfoList = tocSaleImportJobLogMapper.reasonFailureLogExport(logId);
        // 2 改这里 下载的header
        List<String> headers = querySalesBatchReasonFailureLogHeaders;
        ExcelWriter excelWriter;
        if (tocSaleImportJobLogInfoList.size() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(tocSaleImportJobLogInfoList)) {
            for (TocSaleImportJobLogInfo record : tocSaleImportJobLogInfoList) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reasonFailureLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "失败日志");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reasonFailureLogConvertToExcelRow(TocSaleImportJobLogInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getLogId());
        objects.add(record.getName());
        objects.add(record.getShopOrderNo());
        objects.add(record.getFailReason());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(ObjectUtil.isEmpty(record.getOutQty())? "0": record.getOutQty());
        objects.add(ObjectUtil.isEmpty(record.getQty())? "0": record.getQty());
        return objects;
    }

    public ExportInfo exportMateInventorySaleChannelChangeLog(MateInventorySaleChannelChangeLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventorySaleChannelChangeLogQueryRequest firstPageRequest = new MateInventorySaleChannelChangeLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventorySaleChannelChangeLogInfo> firstPage = mateInventorySaleChannelChangeLogFacade.queryMateInventorySaleChannelChangeLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventorySaleChannelChangeLogHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventorySaleChannelChangeLogInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventorySaleChannelChangeLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventorySaleChannelChangeLogQueryRequest pageRequest = new MateInventorySaleChannelChangeLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventorySaleChannelChangeLogInfo> page = mateInventorySaleChannelChangeLogFacade.queryMateInventorySaleChannelChangeLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventorySaleChannelChangeLogInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventorySaleChannelChangeLogConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "货权变更主模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventorySaleChannelChangeLogConvertToExcelRow(MateInventorySaleChannelChangeLogInfo record) {
        List<Object> objects = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        objects.add(record.getOrderNo());
        objects.add(record.getSubWarehouseName());
        objects.add(StringUtils.isNotEmpty(record.getBeforeSaleSubChannel())? saleChannel(null, record.getBeforeSaleSubChannel()): "");
        objects.add(StringUtils.isNotEmpty(record.getAfterSaleSubChannel())? saleChannel(null, record.getAfterSaleSubChannel()): "");
        objects.add(record.getTotalQty());
        objects.add(record.getTotalCostPrice());
        objects.add(record.getTotalExceptCostPrice());
        objects.add((ObjectUtil.isNotEmpty(record.getTotalExceptCostPrice())? record.getTotalExceptCostPrice(): new BigDecimal("0")).subtract((ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): new BigDecimal("0"))));
        objects.add(MateInventorySaleChannelChangeLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName());
        objects.add(record.getCreateUserName());
        objects.add(record.getAuditUserName());
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? sdf.format(record.getCreateTime()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getUpdateTime())? sdf.format(record.getUpdateTime()): "");
        return objects;
    }

    public ExportInfo exportMateInventorySaleChannelChangeModeLog(MateInventorySaleChannelChangeLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventorySaleChannelChangeLogQueryRequest firstPageRequest = new MateInventorySaleChannelChangeLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventorySaleChannelChangeLogModeInfo> firstPage = mateInventorySaleChannelChangeLogFacade.queryMateInventorySaleChannelChangeModeLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventorySaleChannelChangeLogModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventorySaleChannelChangeLogModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventorySaleChannelChangeLogModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventorySaleChannelChangeLogQueryRequest pageRequest = new MateInventorySaleChannelChangeLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventorySaleChannelChangeLogModeInfo> page = mateInventorySaleChannelChangeLogFacade.queryMateInventorySaleChannelChangeModeLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventorySaleChannelChangeLogModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventorySaleChannelChangeLogModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "货权变更货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventorySaleChannelChangeLogModeConvertToExcelRow(MateInventorySaleChannelChangeLogModeInfo record) {
        List<Object> objects = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(StringUtils.isNotEmpty(record.getBeforeSaleSubChannel())? saleChannel(null, record.getBeforeSaleSubChannel()): "");
        objects.add(StringUtils.isNotEmpty(record.getAfterSaleSubChannel())? saleChannel(null, record.getAfterSaleSubChannel()): "");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? sdf.format(record.getCreateTime()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventorySaleChannelChangeLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getRemark())? record.getRemark(): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getQty())? record.getQty(): "0");
        objects.add(ObjectUtil.isNotEmpty(record.getInventoryType())? InventoryTypeEnum.valueOf(record.getInventoryType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCostPrice())? record.getCostPrice(): "0");
        objects.add(ObjectUtil.isNotEmpty(record.getExceptCostPrice())? record.getExceptCostPrice(): "0");
        objects.add((ObjectUtil.isNotEmpty(record.getExceptCostPrice()) ? record.getExceptCostPrice(): BigDecimal.ZERO).subtract(ObjectUtil.isNotEmpty(record.getCostPrice())? record.getCostPrice(): BigDecimal.ZERO));
        objects.add(ObjectUtil.isNotEmpty(record.getExceptSaleTime())? sdf.format(record.getExceptSaleTime()): "");
        objects.add(StringUtils.isNotEmpty(record.getPurchaseCode())? record.getPurchaseCode(): "");
        objects.add(StringUtils.isNotEmpty(record.getProduceCode())? record.getProduceCode(): "");
        objects.add(StringUtils.isNotEmpty(record.getCountryOfOrigin())? record.getCountryOfOrigin(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProductDate())? sdf.format(record.getProductDate()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExpireDate())? sdf.format(record.getExpireDate()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getUpdateTime())? sdf.format(record.getUpdateTime()): "");
        return objects;
    }

    public ExportInfo exportTobSaleReturnOrder(TobSaleReturnOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        TobSaleReturnOrderQueryRequest firstPageRequest = new TobSaleReturnOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<TobSaleReturnModel> firstPage = tobSaleReturnOrderFacade.pageQuery(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryTobSaleReturnOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (TobSaleReturnModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportTobSaleReturnOrderConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            TobSaleReturnOrderQueryRequest pageRequest = new TobSaleReturnOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<TobSaleReturnModel> page = tobSaleReturnOrderFacade.pageQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (TobSaleReturnModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportTobSaleReturnOrderConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "预售单退货");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportTobSaleReturnOrderConvertToExcelRow(TobSaleReturnModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTobSaleOrderNo())? record.getTobSaleOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getPoOrderNo())? record.getPoOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getRemark())? record.getRemark(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getReturnStatus())? TobSaleReturnOrderReturnStatusEnum.valueOf(record.getReturnStatus()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalReturnQty())? record.getTotalReturnQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTakePlace())? record.getTakePlace(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        return objects;
    }

    public ExportInfo exportTobSaleReturnOrderMode(TobSaleReturnOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        TobSaleReturnOrderQueryRequest firstPageRequest = new TobSaleReturnOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<TobSaleReturnModeModel> firstPage = tobSaleReturnOrderFacade.pageModeQuery(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryTobSaleReturnOrderModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (TobSaleReturnModeModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportTobSaleReturnOrderModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            TobSaleReturnOrderQueryRequest pageRequest = new TobSaleReturnOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<TobSaleReturnModeModel> page = tobSaleReturnOrderFacade.pageModeQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (TobSaleReturnModeModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportTobSaleReturnOrderModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "预售单退货");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportTobSaleReturnOrderModeConvertToExcelRow(TobSaleReturnModeModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTobSaleOrderNo())? record.getTobSaleOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getPoOrderNo())? record.getPoOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getRemark())? record.getRemark(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getReturnStatus())? TobSaleReturnOrderReturnStatusEnum.valueOf(record.getReturnStatus()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTakePlace())? record.getTakePlace(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getReturnQty())? record.getReturnQty(): "");
        return objects;
    }

    public ExportInfo exportMateInventoryOtherOutLogExcel(MateInventoryOtherOutLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryOtherOutLogQueryRequest firstPageRequest = new MateInventoryOtherOutLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryOtherOutLogInfo> firstPage = mateInventoryOtherOutLogFacade.queryMateInventoryOtherOutLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryOtherOutLogHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryOtherOutLogInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventoryOtherOutLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryOtherOutLogQueryRequest pageRequest = new MateInventoryOtherOutLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryOtherOutLogInfo> page = mateInventoryOtherOutLogFacade.queryMateInventoryOtherOutLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryOtherOutLogInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventoryOtherOutLogConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "其他出库");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventoryOtherOutLogConvertToExcelRow(MateInventoryOtherOutLogInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalQty())? record.getTotalQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryOtherOutLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderType())? MateInventoryOtherOutLogOrderTypeEnum.valueOf(record.getOrderType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        return objects;
    }

    public ExportInfo exportMateInventoryOtherInLogExcel(MateInventoryOtherInLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryOtherInLogQueryRequest firstPageRequest = new MateInventoryOtherInLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryOtherInLogInfo> firstPage = mateInventoryOtherInLogFacade.queryMateInventoryOtherInLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryOtherInLogHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryOtherInLogInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventoryOtherInLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryOtherInLogQueryRequest pageRequest = new MateInventoryOtherInLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryOtherInLogInfo> page = mateInventoryOtherInLogFacade.queryMateInventoryOtherInLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryOtherInLogInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventoryOtherInLogConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "其他入库");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventoryOtherInLogConvertToExcelRow(MateInventoryOtherInLogInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalQty())? record.getTotalQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryOtherInLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderType())? MateInventoryOtherInLogOrderTypeEnum.valueOf(record.getOrderType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        return objects;
    }

    public ExportInfo exportMateInventoryOtherOutLogModeExcel(MateInventoryOtherOutLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryOtherOutLogQueryRequest firstPageRequest = new MateInventoryOtherOutLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryOtherOutLogModeInfo> firstPage = mateInventoryOtherOutLogFacade.queryMateInventoryOtherOutLogMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryOtherOutLogModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryOtherOutLogModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventoryOtherOutLogModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryOtherOutLogQueryRequest pageRequest = new MateInventoryOtherOutLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryOtherOutLogModeInfo> page = mateInventoryOtherOutLogFacade.queryMateInventoryOtherOutLogMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryOtherOutLogModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventoryOtherOutLogModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "其他出库货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventoryOtherOutLogModeConvertToExcelRow(MateInventoryOtherOutLogModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryOtherOutLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderType())? MateInventoryOtherOutLogOrderTypeEnum.valueOf(record.getOrderType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCostPrice())? record.getCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getQty())? record.getQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getPurchaseCode())? record.getPurchaseCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProduceCode())? record.getProduceCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCountryOfOrigin())? record.getCountryOfOrigin(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProductDate())? record.getProductDate(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExpireDate())? record.getExpireDate(): "");
        return objects;
    }

    public ExportInfo exportMateInventoryOtherInLogModeExcel(MateInventoryOtherInLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryOtherInLogQueryRequest firstPageRequest = new MateInventoryOtherInLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryOtherInLogModeInfo> firstPage = mateInventoryOtherInLogFacade.queryMateInventoryOtherInLogMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryOtherInLogModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryOtherInLogModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = reportMateInventoryOtherInLogModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryOtherInLogQueryRequest pageRequest = new MateInventoryOtherInLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryOtherInLogModeInfo> page = mateInventoryOtherInLogFacade.queryMateInventoryOtherInLogMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryOtherInLogModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = reportMateInventoryOtherInLogModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "其他入库货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> reportMateInventoryOtherInLogModeConvertToExcelRow(MateInventoryOtherInLogModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryOtherInLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderType())? MateInventoryOtherInLogOrderTypeEnum.valueOf(record.getOrderType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getInventoryType())? InventoryTypeEnum.valueOf(record.getInventoryType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getPurchaseCode())? record.getPurchaseCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProduceCode())? record.getProduceCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getRkTime())? record.getRkTime(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCostPrice())? record.getCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getQty())? record.getQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCountryOfOrigin())? record.getCountryOfOrigin(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProductDate())? record.getProductDate(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExpireDate())? record.getExpireDate(): "");
        return objects;
    }

    public ExportInfo exportSaleDemandOrderMode(SaleDemandOrderQueryModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        SaleDemandOrderQueryModeRequest firstPageRequest = new SaleDemandOrderQueryModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<SaleDemandOrderModeInfo> firstPage = saleDemandOrderFacade.querySaleDemandOrderMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = querySaleDemandOrderModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (SaleDemandOrderModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportSaleDemandOrderModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            SaleDemandOrderQueryModeRequest pageRequest = new SaleDemandOrderQueryModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<SaleDemandOrderModeInfo> page = saleDemandOrderFacade.querySaleDemandOrderMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (SaleDemandOrderModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportSaleDemandOrderModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "销售需求货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportSaleDemandOrderModeConvertToExcelRow(SaleDemandOrderModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(ObjectUtil.isNotEmpty(record.getPoOrderNo())? record.getPoOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(saleChannel(record.getSaleChannel(), record.getSaleSubChannel()));
        objects.add(ObjectUtil.isNotEmpty(record.getRemark())? record.getRemark(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCurrencyName())? record.getCurrencyName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExchangeRate())? record.getExchangeRate(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? SaleDemandOrderOrderStatusEnum.getNameByIntValue(record.getOrderStatus()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? record.getCreateTime(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProductSpecification())? record.getProductSpecification(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getGeneraCodeName())? record.getGeneraCodeName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getBrandName())? record.getBrandName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getUnitName())? record.getUnitName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getDemandQty())? record.getDemandQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getDemandAffirmQty())? record.getDemandAffirmQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getLockQty())? record.getLockQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getWorstDemandPeriod())? DateUtil.formatDate(record.getWorstDemandPeriod()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getDemandPeriod())? DateUtil.formatDateTime(record.getDemandPeriod()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getEndTime())? DateUtil.formatDateTime(record.getEndTime()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExceptSalePriceTax())? record.getExceptSalePriceTax(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExceptSalePriceNoTax())? record.getExceptSalePriceNoTax(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExceptSalePriceTaxLocal())? record.getExceptSalePriceTaxLocal(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExceptSalePriceNoTaxLocal())? record.getExceptSalePriceNoTaxLocal(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTaxRate())? record.getTaxRate(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getDiscount())? record.getDiscount(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTargetCostLocal())? record.getTargetCostLocal(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExceptGrossProfit())? record.getExceptGrossProfit(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCount())? record.getCount(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCount()) && record.getCount() != 0 ? "已报价": "已报价");
        objects.add(ObjectUtil.isNotEmpty(record.getDetailOrderStatus())? SaleDemandDetailOrderStatusEnum.valueOf(record.getDetailOrderStatus()).getName(): "");
        return objects;
    }

    public ExportInfo queryMateInventoryCxLogExcel(MateInventoryCxLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryCxLogQueryRequest firstPageRequest = new MateInventoryCxLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryCxLogQueryResponse> firstPage = mateInventoryCxLogFacade.queryMateInventoryCxLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryCxLogHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryCxLogQueryResponse record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportMateInventoryCxLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryCxLogQueryRequest pageRequest = new MateInventoryCxLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryCxLogQueryResponse> page = mateInventoryCxLogFacade.queryMateInventoryCxLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryCxLogQueryResponse record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportMateInventoryCxLogConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存拆卸");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportMateInventoryCxLogConvertToExcelRow(MateInventoryCxLogQueryResponse record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getSubWarehouseName());
        objects.add(record.getChaiQty());
        objects.add(record.getOutQty());
        objects.add(record.getInQty());
        objects.add(MateInventoryCxLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        return objects;
    }

    public ExportInfo queryMateInventoryCxLogModeExcel(MateInventoryCxLogModeQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryCxLogModeQueryRequest firstPageRequest = new MateInventoryCxLogModeQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryCxLogModeInfo> firstPage = mateInventoryCxLogFacade.queryMateInventoryCxLogMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryCxLogModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryCxLogModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportMateInventoryCxLogModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryCxLogModeQueryRequest pageRequest = new MateInventoryCxLogModeQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryCxLogModeInfo> page = mateInventoryCxLogFacade.queryMateInventoryCxLogMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryCxLogModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportMateInventoryCxLogModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存拆卸货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportMateInventoryCxLogModeConvertToExcelRow(MateInventoryCxLogModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getSubWarehouseName());
        objects.add(MateInventoryCxLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getMateSku());
        objects.add(record.getMateSkuName());
        objects.add(saleChannel(null, record.getSaleSubChannel()));
        objects.add(record.getCostPrice());
        objects.add(record.getBeforeQty());
        objects.add(record.getChaiQty());
        objects.add(record.getAfterMateSku());
        objects.add(record.getAfterMateSkuName());
        objects.add(record.getAfterCostPrice());
        objects.add(record.getAfterChaiQty());
        objects.add(record.getPurchaseCode());
        objects.add(InventoryTypeEnum.valueOf(record.getInventoryType()).getName());
        objects.add(record.getProduceCode());
        objects.add(DateUtil.formatDate(record.getProductDate()));
        objects.add(DateUtil.formatDate(record.getExpireDate()));
        return objects;
    }

    public ExportInfo exportPurchaseReturnExcel(PurchaseReturnPageQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        PurchaseReturnPageQueryRequest firstPageRequest = new PurchaseReturnPageQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<PurchaseReturnModel> firstPage = purchaseReturnOrderFacade.pageQuery(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryPurchaseReturnHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (PurchaseReturnModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportPurchaseReturnConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            PurchaseReturnPageQueryRequest pageRequest = new PurchaseReturnPageQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<PurchaseReturnModel> page = purchaseReturnOrderFacade.pageQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (PurchaseReturnModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportPurchaseReturnConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "采购退单");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportPurchaseReturnConvertToExcelRow(PurchaseReturnModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getPurchaseOrderNo());
        objects.add(record.getReason());
        objects.add(record.getRemark());
        objects.add(PurchaseReturnOrderReturnTypeEnum.valueOf(record.getReturnType()).getName());
        objects.add(PurchaseReturnOrderReturnStatusEnum.valueOf(record.getReturnStatus()).getName());
        objects.add(record.getSupplierName());
        objects.add(record.getReturnAmt());
        objects.add(record.getReturnQty());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        return objects;
    }

    public ExportInfo exportPurchaseReturnModeExcel(PurchaseReturnPageModeQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        PurchaseReturnPageModeQueryRequest firstPageRequest = new PurchaseReturnPageModeQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<PurchaseReturnOrderModeInfo> firstPage = purchaseReturnOrderFacade.pageModeQuery(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryPurchaseReturnModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (PurchaseReturnOrderModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportPurchaseReturnModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            PurchaseReturnPageModeQueryRequest pageRequest = new PurchaseReturnPageModeQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<PurchaseReturnOrderModeInfo> page = purchaseReturnOrderFacade.pageModeQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (PurchaseReturnOrderModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportPurchaseReturnModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "采购退单货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportPurchaseReturnModeConvertToExcelRow(PurchaseReturnOrderModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(record.getOrderNo());
        objects.add(record.getPurchaseOrderNo());
        objects.add(record.getReason());
        objects.add(record.getRemark());
        objects.add(PurchaseReturnOrderReturnTypeEnum.valueOf(record.getReturnType()).getName());
        objects.add(PurchaseReturnOrderReturnStatusEnum.valueOf(record.getReturnStatus()).getName());
        objects.add(record.getSupplierName());
        objects.add(record.getCreateUserName());
        objects.add(DateUtil.formatDateTime(record.getCreateTime()));
        objects.add(record.getMateSKu());
        objects.add(record.getMateSKuName());
        objects.add(record.getBrandName());
        objects.add(record.getCategoryName());
        objects.add(record.getProductSpecification());
        objects.add(record.getUnitName());
        objects.add(record.getFinalPurchasePrice());
        objects.add(record.getLockOrderQty());
        objects.add(record.getLessQty());
        objects.add(record.getFinalTotalCostPrice());
        return objects;
    }

    public ExportInfo exportSaleReturnOrder(SaleReturnOrderQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        SaleReturnOrderQueryRequest firstPageRequest = new SaleReturnOrderQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<SaleReturnModel> firstPage = saleReturnOrderFacade.pageQuery(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = querySaleReturnOrderHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (SaleReturnModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportSaleReturnModelConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            SaleReturnOrderQueryRequest pageRequest = new SaleReturnOrderQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<SaleReturnModel> page = saleReturnOrderFacade.pageQuery(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (SaleReturnModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportSaleReturnModelConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "销售单退货");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportSaleReturnModelConvertToExcelRow(SaleReturnModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSaleOrderNo())? record.getSaleOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getShopOrderNo())? record.getShopOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderType())? SaleReturnOrderOrderTypeEnum.valueOf(record.getOrderType()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getRemark())? record.getRemark(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getReturnStatus())? SaleReturnOrderReturnStatusEnum.valueOf(record.getReturnStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalReturnQty())? record.getTotalReturnQty(): "0");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? DateUtil.formatDateTime(record.getCreateTime()): "");
        return objects;
    }

    public ExportInfo exportSaleReturnOrderMode(SaleReturnOrderQueryModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        SaleReturnOrderQueryModeRequest firstPageRequest = new SaleReturnOrderQueryModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<SaleReturnModeModel> firstPage = saleReturnOrderFacade.pageQueryMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = querySaleReturnOrderModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (SaleReturnModeModel record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportSaleReturnOrderModeModelConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            SaleReturnOrderQueryModeRequest pageRequest = new SaleReturnOrderQueryModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<SaleReturnModeModel> page = saleReturnOrderFacade.pageQueryMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (SaleReturnModeModel record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportSaleReturnOrderModeModelConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "销售单退货货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportSaleReturnOrderModeModelConvertToExcelRow(SaleReturnModeModel record) {
        List<Object> objects = new ArrayList<>();
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSaleOrderNo())? record.getSaleOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getShopOrderNo())? record.getShopOrderNo(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderType())? SaleReturnOrderOrderTypeEnum.valueOf(record.getOrderType()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getRemark())? record.getRemark(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getReturnStatus())? SaleReturnOrderReturnStatusEnum.valueOf(record.getReturnStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? DateUtil.formatDateTime(record.getCreateTime()): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(StringUtils.isNotEmpty(record.getProductSpecification())? record.getProductSpecification(): "");
        objects.add(StringUtils.isNotEmpty(record.getBrandName())? record.getBrandName(): "");
        objects.add(StringUtils.isNotEmpty(record.getUnitName())? record.getUnitName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getReturnQty())? record.getReturnQty(): "");
        return objects;
    }

    public ExportInfo exportMateInventoryTypeChangeLogExcel(MateInventoryTypeChangeLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryTypeChangeLogQueryRequest firstPageRequest = new MateInventoryTypeChangeLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryTypeChangeLogInfo> firstPage = mateInventoryTypeChangeLogFacade.queryMateInventoryTypeChangeLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryTypeChangeLogHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryTypeChangeLogInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportMateInventoryTypeChangeLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryTypeChangeLogQueryRequest pageRequest = new MateInventoryTypeChangeLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryTypeChangeLogInfo> page = mateInventoryTypeChangeLogFacade.queryMateInventoryTypeChangeLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryTypeChangeLogInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportMateInventoryTypeChangeLogConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存状态变更");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportMateInventoryTypeChangeLogConvertToExcelRow(MateInventoryTypeChangeLogInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(StringUtils.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getChangeType())? MateInventoryTypeChangeLogChangeTypeEnum.valueOf(record.getChangeType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalQty())? record.getTotalQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryTypeChangeLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? DateUtil.formatDateTime(record.getCreateTime()): "");
        return objects;
    }

    public ExportInfo exportMateInventoryTypeChangeLogModeExcel(MateInventoryTypeChangeLogQueryModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryTypeChangeLogQueryModeRequest firstPageRequest = new MateInventoryTypeChangeLogQueryModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryTypeChangeLogModeInfo> firstPage = mateInventoryTypeChangeLogFacade.queryMateInventoryTypeChangeLogMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryTypeChangeLogModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryTypeChangeLogModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportMateInventoryTypeChangeLogModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryTypeChangeLogQueryModeRequest pageRequest = new MateInventoryTypeChangeLogQueryModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryTypeChangeLogModeInfo> page = mateInventoryTypeChangeLogFacade.queryMateInventoryTypeChangeLogMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryTypeChangeLogModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportMateInventoryTypeChangeLogModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存状态变更货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportMateInventoryTypeChangeLogModeConvertToExcelRow(MateInventoryTypeChangeLogModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(StringUtils.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getChangeType())? MateInventoryTypeChangeLogChangeTypeEnum.valueOf(record.getChangeType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryTypeChangeLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? DateUtil.formatDateTime(record.getCreateTime()): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCostPrice())? record.getCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getQty())? record.getQty(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCostPrice())? record.getTotalCostPrice(): "");
        objects.add(StringUtils.isNotEmpty(record.getPurchaseCode())? record.getPurchaseCode(): "");
        objects.add(StringUtils.isNotEmpty(record.getProduceCode())? record.getProduceCode(): "");
        objects.add(StringUtils.isNotEmpty(record.getCountryOfOrigin())? record.getCountryOfOrigin(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProductDate())? DateUtil.formatDate(record.getProductDate()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExpireDate())? DateUtil.formatDate(record.getExpireDate()): "");
        return objects;
    }

    public ExportInfo exportMateInventoryAdjustLog(MateInventoryAdjustLogQueryRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryAdjustLogQueryRequest firstPageRequest = new MateInventoryAdjustLogQueryRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryAdjustLogInfo> firstPage = mateInventoryAdjustLogFacade.queryMateInventoryAdjustLog(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryAdjustLogHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryAdjustLogInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportMateInventoryAdjustLogConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryAdjustLogQueryRequest pageRequest = new MateInventoryAdjustLogQueryRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryAdjustLogInfo> page = mateInventoryAdjustLogFacade.queryMateInventoryAdjustLog(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryAdjustLogInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportMateInventoryAdjustLogConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存矫正");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportMateInventoryAdjustLogConvertToExcelRow(MateInventoryAdjustLogInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
//        objects.add(ObjectUtil.isNotEmpty(record.getAdjustType())? MateInventoryAdjustLogAdjustTypeEnum.valueOf(record.getAdjustType()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAdjustReason())? record.getAdjustReason(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalQuantity())? record.getTotalQuantity(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getTotalCost())? record.getTotalCost(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryAdjustLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? DateUtil.formatDateTime(record.getCreateTime()): "");
        return objects;
    }

    public ExportInfo exportMateInventoryAdjustLogMode(MateInventoryAdjustLogQueryModeRequest request) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        int pageSize = commonProperties.getMaxExportPageQuerySize();
        MateInventoryAdjustLogQueryModeRequest firstPageRequest = new MateInventoryAdjustLogQueryModeRequest();
        BeanUtils.copyProperties(request, firstPageRequest);
        firstPageRequest.setCurrent(1);
        firstPageRequest.setPageSize(pageSize);
        // 1 改这里 分页查询方法
        Page<MateInventoryAdjustLogModeInfo> firstPage = mateInventoryAdjustLogFacade.queryMateInventoryAdjustLogMode(firstPageRequest);
        // 2 改这里 下载的header
        List<String> headers = queryMateInventoryAdjustLogModeHeaders;
        ExcelWriter excelWriter;
        if (firstPage.getTotal() > commonProperties.getBigExcelWriterRows()) {
            excelWriter = ExcelUtil.getBigWriter();
        } else {
            excelWriter = ExcelUtil.getWriter(true);
        }
        excelWriter.writeHeadRow(headers);
        if (CollectionUtils.isNotEmpty(firstPage.getRecords())) {
            for (MateInventoryAdjustLogModeInfo record : firstPage.getRecords()) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportMateInventoryAdjustLogModeConvertToExcelRow(record);
                excelWriter.writeRow(row);
            }
        }
        for (int i = 2; i <= firstPage.getPages(); i++) {
            MateInventoryAdjustLogQueryModeRequest pageRequest = new MateInventoryAdjustLogQueryModeRequest();
            BeanUtils.copyProperties(request, pageRequest);
            pageRequest.setCurrent(i);
            pageRequest.setPageSize(pageSize);
            // 5 改这里 分页查询方法
            Page<MateInventoryAdjustLogModeInfo> page = mateInventoryAdjustLogFacade.queryMateInventoryAdjustLogMode(pageRequest);
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (MateInventoryAdjustLogModeInfo record : page.getRecords()) {
                    // 6 改这里 查询结果转换成excel内容
                    List<Object> row = exportMateInventoryAdjustLogModeConvertToExcelRow(record);
                    excelWriter.writeRow(row);
                }
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "库存矫正货品模式");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportMateInventoryAdjustLogModeConvertToExcelRow(MateInventoryAdjustLogModeInfo record) {
        List<Object> objects = new ArrayList<>();
        objects.add(StringUtils.isNotEmpty(record.getOrderNo())? record.getOrderNo(): "");
        objects.add(StringUtils.isNotEmpty(record.getSubWarehouseName())? record.getSubWarehouseName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAdjustReason())? record.getAdjustReason(): "");
//        objects.add(ObjectUtil.isNotEmpty(record.getAdjustType())? MateInventoryAdjustLogAdjustTypeEnum.valueOf(record.getAdjustType()).getName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getOrderStatus())? MateInventoryAdjustLogOrderStatusEnum.valueOf(record.getOrderStatus()).getName(): "");
        objects.add(StringUtils.isNotEmpty(record.getCreateUserName())? record.getCreateUserName(): "");
        objects.add(StringUtils.isNotEmpty(record.getAuditUserName())? record.getAuditUserName(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCreateTime())? DateUtil.formatDateTime(record.getCreateTime()): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSku())? record.getMateSku(): "");
        objects.add(StringUtils.isNotEmpty(record.getMateSkuName())? record.getMateSkuName(): "");
        objects.add(StringUtils.isNotEmpty(record.getSaleSubChannel())? saleChannel(null, record.getSaleSubChannel()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getInventoryType())? InventoryTypeEnum.valueOf(record.getInventoryType()): "");
        objects.add(StringUtils.isNotEmpty(record.getPurchaseCode())? record.getPurchaseCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getCostPrice())? record.getCostPrice(): "");
        objects.add(StringUtils.isNotEmpty(record.getProduceCode())? record.getProduceCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getProductDate())? DateUtil.formatDate(record.getProductDate()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getExpireDate())? DateUtil.formatDate(record.getExpireDate()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAdjustQty())? record.getAdjustQty(): "");
        objects.add(StringUtils.isNotEmpty(record.getAfterPurchaseCode())? record.getAfterPurchaseCode(): "");
        objects.add(StringUtils.isNotEmpty(record.getAfterProduceCode())? record.getAfterProduceCode(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAfterCostPrice())? record.getAfterCostPrice(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getDiffTotalCost())? record.getDiffTotalCost(): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAfterProductDate())? DateUtil.formatDate(record.getAfterProductDate()): "");
        objects.add(ObjectUtil.isNotEmpty(record.getAfterExpireDate())? DateUtil.formatDate(record.getAfterExpireDate()): "");
        return objects;
    }

    public ExportInfo exportRepeatSaleOrderNoListExcel(List<String> repeatOrderNoList) {
        init();
        ExportInfo exportInfo = new ExportInfo();
        ExcelWriter excelWriter;
        excelWriter = ExcelUtil.getWriter(true);
        excelWriter.writeHeadRow(Lists.newArrayList("订单号"));
        if (CollectionUtils.isNotEmpty(repeatOrderNoList)) {
            for (String orderNo : repeatOrderNoList) {
                // 4 改这里 查询结果转换成excel内容
                List<Object> row = exportRepeatSaleOrderNoConvertToExcelRow(orderNo);
                excelWriter.writeRow(row);
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        excelWriter.flush(baos, true);
        // 7 改这里 文件名称
        String url = ossTemplateManager.upload(baos, "错误的订单号");
        exportInfo.setDownloadUrl(url);
        return exportInfo;
    }

    private List<Object> exportRepeatSaleOrderNoConvertToExcelRow(String orderNo) {
        List<Object> objects = new ArrayList<>();
        objects.add(orderNo);
        return objects;
    }
}
